<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <script src="/static/js/jquery-3.4.1.min.js"></script>
    <script src="/static/layui/css/layui.css"></script>
    <script src="/static/layui/layui.js"></script>
    <style>
        body {
            margin: 10px;
        }

        .demo-carousel {
            height: 200px;
            line-height: 200px;
            text-align: center;
        }

        .layui-form-item .layui-input-inline {
            float: left;
            width: 140px;
            margin-right: 10px;
        }
    </style>
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">会员</a>
            <a href="">导航</a>
            <a><cite>会员列表</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">会员手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="vipPhone" id="vipPhone" lay-verify="required" autocomplete="off"
                       placeholder="请输入会员手机号" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="button" class="layui-btn" lay-submit="" lay-filter="chaxun" value="查询" id="query_btn">
            </div>
        </div>
    </div>
    <table class="layui-hide" id="demo" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">增加</button>
        <button class="layui-btn  layui-btn-danger  layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-radius" lay-event="detail">积分明细</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    var table;
    layui.use(['layer', 'table', 'jquery', 'laydate'], function () {
        var layer = layui.layer;//弹层
        var $ = layui.$;//使用内置jquery
        var laydate = layui.laydate;
        table = layui.table //表格
        //提示信息
        layer.msg('数据正在加载!');
        //执行一个 table 实例  渲染
        table.render({
            elem: '#demo'
            , id: 'dataListTable'
            , height: 600
            , url: '/VipServlet?action=findAll' //数据接口,数据格式是什么样的?
            , title: '会员表'
            , page: true //开启分页
            , toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , totalRow: true //开启合计行
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'vipID', title: 'ID', width: 60, sort: true, fixed: 'left'}
                , {field: 'vipAut', title: '会员账号', width: 120}
                , {field: 'vipPwd', title: '会员密码', width: 90, sort: true}
                , {field: 'vipPhone', title: '会员手机号', width: 120, sort: true}
                , {field: 'vipIntegral', title: '会员积分', width: 120}
                , {field: 'vipBalance', title: '会员余额', width: 140, sort: true}
                , {field: 'vipGrade', title: '会员等级', width: 150, sort: true}
                , {fixed: 'right', title:'操作', width: 250, align: 'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    //layer.msg('添加');
                    //iframe层-父子操作
                    layer.open({
                        type: 2,
                        area: ['700px', '600px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/pages/vipform.html'
                    });
                    break;

                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //layer.msg('删除');
                        layer.confirm('真的删除行么', function (index) {
                            //将多个ID使用,连接.传到后台
                            console.info(data);
                            let ids = "";
                            for (let i = 0; i < data.length; i++) {
                                ids += data[i].vipID + ",";
                            }
                            //去掉,
                            ids = ids.substr(0, ids.length - 1);
                            console.info("取到的ID是:" + ids);

                            let data_ = {ids: ids};

                            let url = "/VipServlet?action=deleteAll"
                            $.post(url, data_, function (resp) {
                                layer.msg(resp.msg);
                                if (resp.state == 1) {
                                    //重新加载表格
                                    table.reload('dataListTable');
                                }
                            }, "json");
                        });
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
                layer.open({
                    type: 2,
                    area: ['850px', '600px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/pages/integarlMx.html?id1='+data.vipID,
                    success:function (layero,index) {

                    }
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);//关闭弹出框
                    //向服务端发送删除指令
                    let url = "/VipServlet?action=delete"
                    let data_ = {vipID: data.vipID}; //传入的参数
                    console.info(data.vipID);
                    $.post(url, data_, function (p) {
                        if (p.state == 1) {
                            layer.msg('删除成功');
                        } else {
                            layer.msg('删除失败');
                        }
                    }, "json");
                });
            } else if (layEvent === 'edit') {
                //layer.msg('编辑操作');
                //iframe层-父子操作
                layer.open({
                    type: 2,
                    area: ['700px', '600px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/pages/vipform.html?id=' + data.vipID,
                    success: function (layero, index) {
                        //当页面弹出完成时,怎么做
                        //alert(data.userName);
                    }
                });
            }
        });
    });

    $(function () {
        $("#query_btn").on("click", function () {
            var date_ = {
                vipPhone: $("#vipPhone").val(),
            };
            table.reload('dataListTable', {
                page: {
                    curr: 1//重新从第一页开始
                }
                , where: date_ //需要传到后台的参数
            });
        })
    })
</script>
</body>
</html>